/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package image.review;

import image.review.components.ImageComponent;
import image.review.handlers.IOHandler;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;

/**
 *
 * @author troels
 */
public class Gui extends JFrame {

    // menu
    JMenuBar menu;
    JMenu fileMenu;
    JMenuItem fileMenuOpen;
    //image
    File[] images;
    ImageComponent imgc;
    int currentPosition;
    JScrollPane sp;
    
    JPanel thumbView;

    public void display() {

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(350, 150);
        this.setSize(new Dimension(1024, 756));
        //this.setMinimumSize(this.getSize());
        this.setVisible(true);
    }

    public Gui() {
       
        thumbView = new JPanel();
        thumbView.setLayout(new BoxLayout(thumbView, BoxLayout.X_AXIS));
        thumbView.setBackground(Color.red);
        thumbView.add(new JButton("HEJ"));
        
        imgc = new ImageComponent();
        sp = new JScrollPane(imgc);

        menu = new JMenuBar();
        fileMenu = new JMenu("File");
        fileMenuOpen = new JMenuItem("Open directory");
        fileMenuOpen.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                JFileChooser fileDialog = new JFileChooser(new File(""));
                fileDialog.setDialogType(JFileChooser.OPEN_DIALOG);
                fileDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fileDialog.setSize(800, 1200);

                int returnValue = fileDialog.showOpenDialog(menu);

                if (returnValue == JFileChooser.APPROVE_OPTION) {

                    // open directory
                    File file = fileDialog.getSelectedFile();
                    // get files from directoyr
                    images = IOHandler.getFilesInDirectory(file);

                    String[] ext = {"jpg", "png", "bmp", "gif"};
                    images = IOHandler.returnExtensionsOnly(images, ext);

                    currentPosition = 0;

                    changeImage(currentPosition);
                }
                else {
                    // F U !
                }
            }
        });

        fileMenu.add(fileMenuOpen);
        menu.add(fileMenu);

        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        
        layout.setHorizontalGroup(null);
        
        layout.setVerticalGroup(null);
        
        this.setJMenuBar(menu);
        this.add(sp);
        this.add(thumbView);
        this.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                //...
            }

            @Override
            public void keyPressed(KeyEvent e) {

                if (e.getKeyCode() == KeyEvent.VK_LEFT) {

                    changeImage(currentPosition - 1);
                    currentPosition--;
                }
                else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

                    changeImage(currentPosition + 1);
                    currentPosition++;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //...
            }
        });

        this.addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent e) {

                //
            }

            @Override
            public void componentMoved(ComponentEvent e) {
                //
            }

            @Override
            public void componentShown(ComponentEvent e) {
                //
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                //
            }
        });
    }

    private void changeImage(int index) {

        if (index < 0) {

            index = images.length - 1;
            currentPosition = index;
        }

        if (index >= images.length) {

            index = 0;
            currentPosition = index;
        }

        imgc.setImg(images[index]);
        
        double r = ((double) imgc.getImg().getHeight() / (double) 500);

        imgc.scale(r);
        imgc.repaint();
        imgc.revalidate();
    }
}
